package com.jming.jike.controller.front;

import com.jming.jike.common.ResponseResult;
import com.jming.jike.common.ValidatorGroup.Query;
import com.jming.jike.common.ValidatorGroup.Update;
import com.jming.jike.dto.front.UserDTO;
import com.jming.jike.dto.front.UserLoginDTO;
import com.jming.jike.entity.User;
import com.jming.jike.service.front.UserService;
import com.jming.jike.util.CommonUtils;
import com.jming.jike.vo.front.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/05/25
 */
@RestController
@RequestMapping("/user")
@Api(tags = "前台用户相关接口", value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private CommonUtils commonUtils;

    @Resource
    private HttpSession httpSession;

    @PostMapping("/getCode")
    @ApiOperation(value = "获取验证码接口", notes = "获取验证码接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> getCode(@RequestBody @Validated(Query.class) UserLoginDTO userLoginDTO) {

        return ResponseResult.success(userService.getCode(userLoginDTO));
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "员工登录", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> login(@RequestBody @Validated(Update.class) UserLoginDTO userLoginDTO) {

        User user = userService.login(userLoginDTO);
        // 将登录信息放入session中
        httpSession.setAttribute("User", user);
        // 设置 7天无任何操作自动退出
        httpSession.setMaxInactiveInterval(60 * 60 * 24 * 7);
        return ResponseResult.success(true);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "用户退出", notes = "用户退出", produces = "application/json", httpMethod = "POST")
    public ResponseResult<String> logout(){

        //清理Session中保存的当前登录信息
        httpSession.removeAttribute("User");
        return ResponseResult.success("退出成功");
    }

    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> updateUserInfo(@RequestBody @Validated(Update.class) UserDTO userDTO) {

        return ResponseResult.success(userService.updateUserInfo(userDTO));
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取个人信息", notes = "员工登录", produces = "application/json", httpMethod = "GET")
    public ResponseResult<UserVO> getUserInfo() {

        return ResponseResult.success(userService.getUserInfo());
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传头像", notes = "上传头像", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Map<String, String>> uploadImage(MultipartFile file) {

        return ResponseResult.success(commonUtils.uploadImage(file, "avatar"));
    }

}
